#include <boost/asio.hpp>
#include <boost/asio/use_awaitable.hpp>
#include <boost/asio/co_spawn.hpp>
#include <boost/asio/detached.hpp>
#include <iostream>
#include <stdio.h>
#include <time.h>

using namespace std;
namespace asio = boost::asio;

asio::awaitable<void> async_timer_task(int id, int delay)
{
    printf("time:%ld id:%d start, delay:%d\n", time(NULL), id, delay);
    asio::steady_timer timer(co_await asio::this_coro::executor);
    timer.expires_after(std::chrono::seconds(delay));
    co_await timer.async_wait(asio::use_awaitable);
    printf("time:%ld id:%d end\n", time(NULL), id);
}

int main()
{
    asio::io_context io_ctx;
    asio::co_spawn(io_ctx, async_timer_task(1, 3), asio::detached);
    asio::co_spawn(io_ctx, async_timer_task(2, 4), asio::detached);
    asio::co_spawn(io_ctx, async_timer_task(3, 5), asio::detached);

    io_ctx.run();
    return 0;
}

/* 
time:1758558270 id:1 start, delay:3
time:1758558270 id:2 start, delay:4
time:1758558270 id:3 start, delay:5
time:1758558273 id:1 end
time:1758558274 id:2 end
time:1758558275 id:3 end

测试结论: 多个协程可以同时启动, 并行异步执行
*/